﻿using System;
using System.Collections.Generic;
using System.Text;

namespace iPdf
{
    class PublishPage: TataRibbonPage
    {
        #region Properties
        public override FileFormat SourceFormat
        {
            get
            {
                return SelectedService.SupportedFileFormats;
            }
        }

        protected CloudStorageService SelectedService
        {
            get
            {
                return (CloudStorageService)Service.SelectedItem;
            }
        }

        protected override PageOption PageOption
        {
            get
            {
                return new PublisherOption(SelectedService, Username.Text, Password.Text);
            }
        }

        public override bool ReadyToAct
        {
            get
            {
                return base.ReadyToAct && Username.IsValidated && Password.IsValidated;
            }
        }

        public override string ActionName
        {
            get
            {
                return base.ActionName + "To" +
                            Service.SelectedItem.ToString().Replace(Helper.Space, string.Empty);
            }
        }

        public override string IntroInfo
        {
            get
            {
                return global::iPdf.Properties.Resources.PublishPageIntroInfo;
            }
        }

        #endregion

        #region Ctor
        public PublishPage()
            : base()
        {
            foreach (CloudStorageService service in supportedServices)
            {
                Service.Items.Add(service);
            }

            Service.SelectedItem = supportedServices[0];
            Service.SelectedIndexChanged += new EventHandler(Service_SelectedIndexChanged);
            UpdateValidators();

            actor = new Publisher();
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        protected override void InitializeComponent()
        {
            this.ServiceGroup = new Elegant.Ui.RibbonGroup();
            this.Service = new Elegant.Ui.ComboBox();
            this.Username = new TextBoxWithValidator();
            this.Password = new TextBoxWithValidator();
            this.ChooseFolder = new Elegant.Ui.Button();
            ((System.ComponentModel.ISupportInitialize)(this.ServiceGroup)).BeginInit();
            this.ServiceGroup.SuspendLayout();
            this.SuspendLayout();

            this.Controls.Add(this.ServiceGroup);

            // 
            // ServiceGroup
            // 
            this.ServiceGroup.Controls.Add(this.Service);
            this.ServiceGroup.Controls.Add(this.Username);
            this.ServiceGroup.Controls.Add(this.Password);
            this.ServiceGroup.Controls.Add(this.ChooseFolder);
            this.ServiceGroup.DialogLauncherButtonVisible = false;
            this.ServiceGroup.Id = "2fc46926-af4e-4e56-bb50-69c5d76fdd38";
            this.ServiceGroup.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Service)});
            this.ServiceGroup.Location = new System.Drawing.Point(4, 3);
            this.ServiceGroup.Name = "ServiceGroup";
            this.ServiceGroup.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Service16)});
            this.ServiceGroup.Size = new System.Drawing.Size(213, 94);
            this.ServiceGroup.TabIndex = 0;
            this.ServiceGroup.Text = "Service";
            // 
            // Service
            // 
            this.Service.DrawMode = System.Windows.Forms.DrawMode.Normal;
            this.Service.DroppedDown = false;
            this.Service.Editable = false;
            this.Service.FormatInfo = null;
            this.Service.FormatString = "";
            this.Service.FormattingEnabled = false;
            this.Service.Id = "c7418d7c-7780-4426-96ab-06288d5b5851";
            this.Service.InformativenessMinimumLevel = "Elegant.Ui.ComboBoxInformativenessLevel:ComboBoxWithLabel";
            this.Service.LabelText = "Service";
            this.Service.Location = new System.Drawing.Point(4, 2);
            this.Service.Name = "Service";
            this.Service.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Service16)});
            this.Service.Size = new System.Drawing.Size(200, 24);
            this.Service.Sorted = false;
            this.Service.TabIndex = 1;
            this.Service.TextEditorWidth = 125;
            // 
            // Username
            // 
            this.Username.Id = "d9a44522-7cf5-4f03-b6f2-5dbc3f178461";
            this.Username.ImeMode = System.Windows.Forms.ImeMode.Disable;
            this.Username.InformativenessMaximumLevel = "Elegant.Ui.TextBoxInformativenessLevel:TextBoxWithLabel";
            this.Username.InformativenessMinimumLevel = "Elegant.Ui.TextBoxInformativenessLevel:TextBoxWithLabel";
            this.Username.LabelAreaWidthTemplate = "Username ";
            this.Username.LabelText = "Username";
            this.Username.Location = new System.Drawing.Point(4, 26);
            this.Username.Name = "Username";
            this.Username.Size = new System.Drawing.Size(200, 24);
            this.Username.TabIndex = 2;
            this.Username.TextChanged += new EventHandler(TextBox_TextChanged);
            this.Username.TextEditorWidth = 138;
            // 
            // Password
            // 
            this.Password.Id = "461a6435-6b8d-41a6-9930-57aace3faa2a";
            this.Password.ImeMode = System.Windows.Forms.ImeMode.Disable;
            this.Password.InformativenessMaximumLevel = "Elegant.Ui.TextBoxInformativenessLevel:TextBoxWithLabel";
            this.Password.InformativenessMinimumLevel = "Elegant.Ui.TextBoxInformativenessLevel:TextBoxWithLabel";
            this.Password.LabelAreaWidthTemplate = "Username ";
            this.Password.LabelText = "Password ";
            this.Password.Location = new System.Drawing.Point(4, 50);
            this.Password.Name = "Password";
            this.Password.PasswordChar = '●';
            this.Password.Size = new System.Drawing.Size(200, 24);
            this.Password.TabIndex = 3;
            this.Password.TextChanged += new EventHandler(TextBox_TextChanged);
            this.Password.TextEditorWidth = 138;
            this.Password.UseSystemPasswordChar = true;
            // 
            // ChooseFolder
            // 
            this.ChooseFolder.Id = "c411340b-3625-449a-8bce-54b802496ee3";
            this.ChooseFolder.InformativenessMaximumLevel = "Elegant.Ui.RibbonGroupButtonInformativenessLevel:LargeImageWithText";
            this.ChooseFolder.InformativenessMinimumLevel = "Elegant.Ui.RibbonGroupButtonInformativenessLevel:LargeImageWithText";
            this.ChooseFolder.Location = new System.Drawing.Point(145, 2);
            this.ChooseFolder.Name = "ChooseFolder";
            this.ChooseFolder.Size = new System.Drawing.Size(146, 24);
            this.ChooseFolder.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.ChooseServiceFolder)});
            this.ChooseFolder.TabIndex = 4;
            this.ChooseFolder.Text = "Choose Folder...";

            ((System.ComponentModel.ISupportInitialize)(this.ServiceGroup)).EndInit();
            this.ServiceGroup.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        #region Methods

        public override void ExportSession(System.Xml.XmlElement sessionNode)
        {
            SetXmlElementAttribute(sessionNode, Service.Name, Service.Text);
            SetXmlElementAttribute(sessionNode, Username.Name, Username.Text);
            SetXmlElementAttribute(sessionNode, Password.Name, Password.Text);
        }

        public override void ImportSession(System.Xml.XmlElement sessionNode)
        {
            string service = sessionNode.GetAttribute(Service.Name);
            if (!string.IsNullOrEmpty(service))
            {
                foreach (CloudStorageService item in Service.Items)
                {
                    if (item.ToString() == service)
                    {
                        Service.SelectedItem = item;
                        break;
                    }
                }
            }

            string username = sessionNode.GetAttribute(Username.Name);
            if (username != null)
            {
                Username.Text = username;
            }
            string password = sessionNode.GetAttribute(Password.Name);
            if (password != null)
            {
                Password.Text = password;
            }
        }

        private void Service_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateValidators();
        }

        private void UpdateValidators()
        {
            Username.Validator = SelectedService.UsernameValidator;
            Password.Validator = SelectedService.PasswordValidator;
        }

        #endregion

        #region Fields
        private Elegant.Ui.RibbonGroup ServiceGroup;
        private Elegant.Ui.ComboBox Service;
        private TextBoxWithValidator Username;
        private TextBoxWithValidator Password;
        private Elegant.Ui.Button ChooseFolder;

        private CloudStorageService[] supportedServices = new CloudStorageService[]
            {
                GoogleDocsPublisher.Instance
            };
    #endregion
    }
}
